import request, {
    IP
} from '@utils/request';
import * as actionTypes from './actionType';
import {
    message
} from 'antd';
import moment from 'moment';
import { createLogger } from 'redux-logger';
import { get } from '../../../utils/request';

//获取所有巡线工告警
export const getWarningBypatroller = (page) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warning/getWarningBypatroller', {
            cmd: 'getWarningBypatroller',
            type: 'request',
            request: {
                ...page
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let totalCount = res.totalCount;
                let result = res.message;
                dispatch(getWarningBypatrollerReducer(result, totalCount))
            } else {
                message.error('获取告警信息失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

const getWarningBypatrollerReducer = (LinerWarning, totalCount) => ({
    type: actionTypes.GET_WARNING,
    LinerWarning,
    totalCount
})

//根据状态类型时间获取告警
export const getWarningByASTTMD = (information) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warning/getWarningByTypeStatusTime', {
            cmd: 'getWarningByTypeStatusTime',
            type: 'request',
            request: {
                ...information
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let totalCount=res.totalCount
                let result=res.message
                dispatch(getWarningBypatrollerReducer(result,totalCount))
            } else {
                message.error('获取告警信息失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

export const getSuspectedStatus = (page) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warning/getWarningCountByPatroller', {
            cmd: 'getWarningCountByPatroller',
            type: 'request',
            request: {
                ...page
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let treatedDataSize = res.treatedDataSize;
                let untreatedDataSize = res.untreatedDataSize;
                let result =res.message
                dispatch(getCounterReducer(treatedDataSize, untreatedDataSize))
                dispatch(getSuspectedStatusReducer(treatedDataSize, untreatedDataSize,result))
            } else {
                message.error('获取告警信息失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

export const getCounter = () => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warning/getWarningCountByPatroller', {
            cmd: 'getWarningCountByPatroller',
            type: 'request',
            request: {
                status:''
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let treatedDataSize = res.treatedDataSize;
                let untreatedDataSize = res.untreatedDataSize;
                dispatch(getCounterReducer(treatedDataSize, untreatedDataSize))
            } else {
                message.error('获取处理结果' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

const getCounterReducer=(treatedDataSize, untreatedDataSize)=>({
    type: actionTypes.GET_COUNTER,
    treatedDataSize,
    untreatedDataSize,
})

const getSuspectedStatusReducer = (treatedDataSize, untreatedDataSize,LinerWarning,totalCount) => ({
    type: actionTypes.GET_SUSPECTED_STATUS,
    treatedDataSize,
    untreatedDataSize,
    LinerWarning,
    totalCount
})


//处理告警
export const feedBack = (details,pageIndex) => {
    let page = {
        pageIndex: pageIndex-1,
        pageSize: 10
    }
    let get={
        ...page,
        status:""
    }
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warning/feedBack', {
            cmd: 'feedBack',
            type: 'request',
            request: {
                ...details
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                message.success('处理成功', 1);
                dispatch(getWarningBypatroller(page))
                dispatch(getCounter(get))
            } else {
                message.error('处理失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

export const modifyPassword = (information) => {   
    
    request('POST', '/api/server/web/rest/user/modifyPassword', {
        cmd: 'modifyPassword',
        type: 'request',
        request: {
            ...information
        }
    }).then(obj => {
        console.log(obj, 'roles')
        let res = obj.response;
        if (res.res) {
            message.success('修改密码成功', 1);
            
        } else {
            message.error('修改密码失败' + res.exception, 1);
        }
    }).catch(err => console.log(err));

}
// //将时间戳改成正常时间
// const modifyingtimestamps = (time) => {
//     let array = time
//     for (var i = 0; i < time.length; i++) {
//         array[i].distributeTime = moment(time[i].distributeTime).format('YYYY-MM-DD HH:mm')
//         array[i].warningTime = moment(time[i].warningTime).format('YYYY-MM-DD HH:mm')
//         array[i].feedbackTime = moment(time[i].feedbackTime).format('YYYY-MM-DD HH:mm')
//     }
//     return array
// }